﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QLPKWeb.Models.FormBean.PhanQuyen;
using DONGACS.Common;
using DONGACS.DTO;
using DONGACS.SERVICE;

namespace QLPKWeb.BussinessDelegate.PhanQuyen
{
    public class RolesServiceDelegate
    {
        RolesService RolesService = new RolesService();
        PathsService PathsService = new PathsService();
        RolesInPathsService RolesInPathsService = new RolesInPathsService();
        UsersInRolesService UsersInRolesService = new UsersInRolesService();

        public IList<RolesDto> GetAllRoles()
        {
            return RolesService.GetAllRoles();
        }

        public IList<RolesDto> GetAllRolesIsEnable()
        {
            return RolesService.GetAllRolesIsEnable();
        }

        public RolesDto GetRolesByName(string name)
        {
            return RolesService.GetRolesByName(name);
        }

        public RolesDto GetRolesByID(string id)
        {
            return RolesService.GetRolesByID(id);
        }

        public RolesDto GetRolesDeletedByName(string name)
        {
            return RolesService.GetRolesByName(name);
        }

        public bool AddNewRoles(RolesDto rolesDto)
        {
            return RolesService.AddNewRoles(rolesDto);
        }

        public bool Update(RolesDto rolesDto)
        {
            return RolesService.Update(rolesDto);
        }

        public bool DeleteRoles(string ID)
        {
            return RolesService.DeleteRoles(ID);
        }

        public IList<PathsDto> GetAllPathsByModulesAndPermissionType(string modules, string permissionType)
        {
            return this.PathsService.GetAllPathsByModulesAndPermissionType(modules, permissionType);
        }

        public RolesDto BuildNewRoles(RolesForm form)
        {
            RolesDto rolesDto = new RolesDto();

            rolesDto.RoleName = form.RoleName;
            rolesDto.LoweredRoleName = form.RoleName.ToLower();
            rolesDto.Description = form.Description;
            rolesDto.IsDelete = false;
            rolesDto.IsEnable = form.IsEnable;
            rolesDto.CreateDate = DateTime.Now;
            rolesDto.UpdateDate = DateTime.Now;

            rolesDto.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            rolesDto.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);

            return rolesDto;
        }

        
        public RolesDto BuildEditRoles(RolesForm form)
        {
            RolesDto rolesDto = new RolesDto();

            rolesDto = RolesService.GetRolesByID(form.ID.ToString());// Lấy thông tin cũ của role trong DataBase

            //Cập nhật lại một số thuộc tính mới
            rolesDto.RoleName = form.RoleName;
            rolesDto.LoweredRoleName = form.RoleName.ToLower();
            rolesDto.Description = form.Description;
            rolesDto.IsDelete = false;
            rolesDto.IsEnable = form.IsEnable;
            rolesDto.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            rolesDto.UpdateDate = DateTime.Now;

            return rolesDto;
        }

        public bool ShiftDelete(string roleID)
        {
            bool result = false;
            result = RolesService.ShiftDelete(roleID); //Xóa role

            //Xóa mọi Paths đã gán cho role
            if(result == true )
                RolesInPathsService.DeleteRolesInPathsByRolesId(roleID);

            return result;
        }

        public IList<PathsDto> GetPaths()
        {
            return this.PathsService.GetAllPaths();
        }

        public IList<RolesInPathsDto> GetRolesInPathsByRolesId(string roleID)
        {
            return this.RolesInPathsService.GetRolesInPathsByRolesId(roleID);
        }

        public void UpdateManageRolesWithPermissions(IList<RolesInPathsDto> rolesInPathsDtos, string roleId)
        {
            this.RolesService.UpdateManageRolesWithPages(rolesInPathsDtos, roleId);
        }

        public IList<UsersInRolesDto> GetUsersInRolesByRoleId(string roleId)
        {
            return UsersInRolesService.GetUsersInRolesByRoleId(roleId);
        }
    }
}
